package com.nc.mem.serial.graph.common;

import static com.nc.mem.serial.graph.Representation.readLong;
import static com.nc.mem.serial.graph.Representation.writeLong;

import java.nio.ByteBuffer;
import java.util.Calendar;
import java.util.TimeZone;

import com.nc.mem.serial.graph.GraphSerializer;


public final class CalendarSerializer extends GraphSerializer {

	public static final CalendarSerializer INSTANCE = new CalendarSerializer();

	private CalendarSerializer() {
	}

	@Override
	public void doRead(final ByteBuffer src, final Object obj) {
		final Calendar cal = (Calendar) obj;
		cal.setTimeInMillis(readLong(src));
	}

	@Override
	public void doWrite(final ByteBuffer dest, final Object obj) {
		final Calendar cal = (Calendar) obj;
		TimeZoneSerializer.INSTANCE.writeNonRoot(dest, cal.getTimeZone());
		writeLong(dest, cal.getTimeInMillis());
	}

	// This can be broken. we should use the Locale too, however there's simple way to get the
	// Locale from a Calendar
	@Override
	public Object instantiate(final ByteBuffer src) {
		return Calendar.getInstance((TimeZone) TimeZoneSerializer.INSTANCE.readNonRoot(src));
	}

}
